from DataManagement.resource import ModelResource
from DataManagement.import_export import ModelField
from gxjg.models import *
from gxdm.models import *


class Gxjg0101Resource(ModelResource):
    id = ModelField(attribute='gh', column_name='工号')
    xm = ModelField(attribute='xm', column_name='姓名')
    dwh = ModelField(attribute='dwh', column_name='单位')
    xbm = ModelField(attribute='xbm', column_name='性别')
    sfzjlxm = ModelField(attribute='sfzjlxm', column_name='身份证件类型')
    sfzjh = ModelField(attribute='sfzjh', column_name='身份证件号')
    zgxl = ModelField(attribute='zgxl', column_name='最高学历')
    jzglbm = ModelField(attribute='jzglbm', column_name='教职工类别')
    type = ModelField(attribute='type', column_name='类型', choices={'教职工': '1', '员工': '2', '部门': '3','外聘教师':'4'})
    yrxs = ModelField(attribute='yrxs', column_name='用人形式')
    dqztm = ModelField(attribute='dqztm', column_name='当前状态', choices={'在职': '1', '离校': '2', '退休': '3', '试用期': '4', '离岗': '5'})
    source = ModelField(attribute='source', column_name='数据来源')
    openoa = ModelField(attribute='openoa', column_name='是否开通oa', choices={'否': '0', '是': '1'})

    class Meta:
        model = Gxjg0101
        fields = ('id', 'dwh', 'xm', 'xbm', 'sfzjlxm', 'sfzjh', 'zgxlm', 'jzglbm', 'type', 'dqztm')
        skip_unchanged = True
        exclude = ('source', 'priority', 'lock', 'mode', 'remarks', 'create_time', 'update_time')

    def before_import_row(self, row, **kwargs):
        row['单位'] = Dwbmdm.objects.filter(label=row['单位']).first()
        row['性别'] = Xbdm.objects.filter(label=row['性别']).first()
        row['身份证件类型'] = Sfzjlxdm.objects.filter(label=row['身份证件类型']).first()
        row['教职工类别'] = Jzglbdm.objects.filter(label=row['教职工类别']).first()
        row['类型'] = self.fields['type'].choices[row['类型']]
        row['当前状态'] = self.fields['dqztm'].choices[row['当前状态']]
        row['是否开通oa'] = self.fields['openoa'].choices[row['是否开通oa']]
        super().before_import_row(row, **kwargs)
